﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Xml.Serialization;

using Webgame.Control;

namespace Virus
{
    /// <summary>
    /// ws 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService()]
    public class ws : System.Web.Services.WebService
    {

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <param name="color"></param>
        /// <returns>如果注册成功，返回空串，如果失败，则返回具体失败的值</returns>
        [WebMethod]
        public string Register(string name, string password, string color)
        {
            PlayerManager.emRegisterResult ret;

            ret = new PlayerManager().Register(name, password, color, "");
            switch (ret)
            {
                case PlayerManager.emRegisterResult.Success:
                    return string.Empty;
                case PlayerManager.emRegisterResult.NameExists:
                    return "名字重复";
                case PlayerManager.emRegisterResult.PasswordEmpty:
                    return "密码不能为空";
                case PlayerManager.emRegisterResult.NameEmpty:
                    return "用户名不能为空";
                default:
                    return "注册失败";
            }
        }

        /// <summary>
        /// 确认一个用户名是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [WebMethod]
        public bool CheckNameExists(string name)
        {
            if (Webgame.Access.Player.Instance.GetPlayerByName(name) != null)
                return true;
            return false;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [WebMethod(EnableSession=true)]
        public Webgame.Entity.Player Login(string name, string password)
        {
            Webgame.Entity.Player player = new PlayerManager().Login(name, password);
            if (player != null)
            {
                player.Password = string.Empty;
                Player = player;
                return player;
            }
            else
            {
                return new Webgame.Entity.Player();
            }
        }

        /// <summary>
        /// 玩家退出登录服务器
        /// </summary>
        [WebMethod(EnableSession = true)]
        public void Logout()
        {
            Player = null;
        }
        /// <summary>
        /// Session里玩家的实体类
        /// 只要玩家登录过服务器，Session就会记录下玩家的实体类信息
        /// </summary>
        Webgame.Entity.Player Player
        {
            get
            {
                object p = Session["Player"];
                if (p == null)
                    return null;
                return p as Webgame.Entity.Player;
            }
            set
            {
                Session["Player"] = value;
            }
        }

        /// <summary>
        /// 获得某个点周围的病毒
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        [WebMethod(EnableSession=true)]
        public VirusResult GetVirus(int x, int y, int mapID)
        {
            Virus.Game.Virus vc = Virus.Game.Virus.GetVirus(0);
            vc.Do();
            

            VirusResult ret = new VirusResult();
            ret.X = vc.ReSetPointX(x);
            ret.Y = vc.ReSetPointY(y);
            ret.LessTime = (int)((TimeSpan)(vc.NextDoTime - DateTime.Now)).TotalSeconds + 2;
            ret.Width = vc.Width;
            ret.Height = vc.Height;

            if (Player != null)
            {
                //  如果玩家登录过，则返回玩家信息
                Webgame.Entity.PlayerScore ps = vc.GetPlayerScore(Player.ID);
                ret.Nutrient = ps.Nutrient;
                ret.PlayerEat = ps.Eat;
                ret.PlayerVirus = ps.Virus;
                //  如果玩家当前没有足够投放1个的养分，并且，当前状态没有病毒，则给一定的养分
                if (ps.Nutrient < 10 && ps.Virus == 0)
                    ps.Nutrient = Webgame.Config.MapConfig.CostNutrient * 10;
            }

            Dictionary<int, VirusResult.Player> playerMap = new Dictionary<int, VirusResult.Player>();

            foreach (Webgame.Entity.VirusStatus v in vc.GetVirus(ret.X, ret.Y, 0))
            {
                Webgame.Entity.Player p = vc.GetPlayer(v.pID);

                VirusResult.Virus virus = new VirusResult.Virus();
                virus.X = v.X;
                virus.Y = v.Y;
                virus.pID = p.ID;
                virus.width = v.Width;
                
                if (!playerMap.ContainsKey(p.ID))
                {
                    VirusResult.Player vp = new VirusResult.Player();
                    vp.Name = p.Name;
                    vp.Color = p.Color;
                    Webgame.Entity.PlayerScore ps = vc.GetPlayerScore(v.pID);
                    vp.Eat = ps.Eat;
                    vp.Virus = ps.Virus;
                    vp.ID = p.ID;

                    playerMap.Add(p.ID, vp);
                    ret.player.Add(vp);
                }

                ret.virus.Add(virus);
            }

            return ret;
        }

        /// <summary>
        /// 投放病毒
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="mapID"></param>
        /// <returns></returns>
        [WebMethod(EnableSession=true)]
        public string AddVirus(int x, int y, int mapID)
        {
            if (Player == null)
                return "未登录";

            Virus.Game.Virus vc = Virus.Game.Virus.GetVirus(0);
            if (!vc.AddVirus(x, y, Player.ID))
                return "病毒投放失败";

            return string.Empty;
        }

        /// <summary>
        /// 玩家使用道具
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="type"></param>
        /// <param name="mapID"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public string UseItem(int x, int y, int type, int mapID)
        {
            if (Player == null)
                return "未登录";

            if (type == 0)
            {
                if (!Virus.Game.Virus.GetVirus(mapID).扩散(Player.ID, x, y))
                    return "扩散病毒失败,可能原因是养分不够";
            }
            else if (type == 1)
            {
                if (!Virus.Game.Virus.GetVirus(mapID).强心针(Player.ID, x, y))
                    return "强心针激活病毒失败，可能是养分不够";
            }
            else if (type == 2)
            {
                if (!Virus.Game.Virus.GetVirus(mapID).无敌(Player.ID, x, y))
                    return "病毒无敌化失败，可能是养分不够";
            }
            else if (type == 3)
            {
                if (!Virus.Game.Virus.GetVirus(mapID).超级分裂(Player.ID, x, y))
                    return "强化病毒失败，可能是养分不够";
            }
            else
            {
                return "未知道具类型";
            }
            return string.Empty;
        }

        /// <summary>
        /// 更换当前的颜色
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public string ChangeColor(string color)
        {
            if (Player == null)
                return "未登录";

            Player.Color = color.Replace("#", "");

            Webgame.Access.Player.Instance.Update(Player);

            Game.Virus.DeletePlayerCache(Player.ID);

            return string.Empty;
        }

        public class VirusResult
        {
            public int X, Y;

            public List<Virus> virus = new List<Virus>();

            public List<Player> player = new List<Player>();

            public class Virus
            {
                public int X, Y;

                public int pID;

                public int width;
            }

            [XmlRoot(Namespace="7day")]
            public class Player
            {
                public int ID;

                /// <summary>
                /// 玩家的名字
                /// </summary>
                public string Name;

                /// <summary>
                /// 玩家病毒的颜色
                /// </summary>
                public string Color;

                /// <summary>
                /// 玩家吞噬病毒的数量
                /// </summary>
                public int Eat;

                /// <summary>
                /// 当前拥有的病毒数量
                /// </summary>
                public int Virus;
            }

            public int LessTime = 0;

            /// <summary>
            /// 玩家在当前地图剩余的养分
            /// </summary>
            public int Nutrient = 0;

            /// <summary>
            /// 病毒已经吞噬过的病毒数量
            /// </summary>
            public int PlayerEat = 0;

            /// <summary>
            /// 玩家当前的病毒数量
            /// </summary>
            public int PlayerVirus = 0;

            public int Width = 0;

            public int Height = 0;
        }
    }
}
